from django.shortcuts import render, HttpResponse, redirect
from django.http import JsonResponse

# Create your views here.
def index(request):
  return HttpResponse("欢迎使用哈哈")

def user_list(request):
  # 默认情况下django会去app下的templates目录中寻找相应的html文件
  return render(request, "user_list.html")

def user_add(request):
  return HttpResponse("添加用户")

def template_test(request):
  name = "zhangsan"
  roles = ["admin", "superadmin", "dev"]
  user_info = {
    "name": "lisi",
    "salary": 10000,
    "role": "CTO"
  }
  data_list = [
    {"name": "wangwu", "age": 18, "gender": "male"},
    {"name": "zhaoliu", "age": 20, "gender": "female"},
    {"name": "zhangqi", "age": 22, "gender": "male"}
  ]
  return render(request, "template_test.html", {"name": name, "roles": roles, "user_info": user_info, "data_list": data_list})


def req_test(request):
  print(request.path)
  print(request.method)
  print(request.GET)
  print(request.POST)
  return HttpResponse("返回内容")

def redirect_test(request):
  return redirect("https://www.baidu.com")

from app_01 import models
def orm_test(request):
  # 测试ORM操作表中的数据
  models.Department.objects.create(title="开发部")
  models.Department.objects.create(title="销售部")
  models.Department.objects.create(title="市场部")
  return HttpResponse("成功！")

def remove_test(request):
  # 删除id=1的数据
  models.UserInfo.objects.filter(id=1).delete()
  # 删除所有数据
  models.Department.objects.all().delete()
  return HttpResponse("删除成功")

def user_add(request):
  models.UserInfo.objects.create(name="zhangsan", password="123", age=18)
  models.UserInfo.objects.create(name="lisi", password="123", age=20)
  models.UserInfo.objects.create(name="wangwu", password="123", age=22)
  return HttpResponse("添加成功")

def user_list(request):
  # 这里user_list是一个QuerySet类型，就相当于一个列表，列表中的每一个元素是models.UserInfo类型
  user_list = models.UserInfo.objects.all()
  for user in user_list:
    print(user.id, user.name, user.password, user.age)
  return HttpResponse("查询成功！")

def get_user_by_id(request):
  # 即使只有一条数据，返回的已然是一个数组
  user_list = models.UserInfo.objects.filter(id=2)
  for user in user_list:
    print(user.id, user.name, user.password, user.age)

  print("使用get***********************************************************")
   # 除非我们确信，只需要一条数据，那么可以这样写
  user_obj = models.UserInfo.objects.get(id=2)
  print(user_obj.id, user_obj.name, user_obj.password, user_obj.age)

  return HttpResponse("查询成功！")

def update_user_by_id(request):
  models.UserInfo.objects.filter(id=2).update(age=25)
  return HttpResponse("更新成功！")

def update_all_user(request):
  models.UserInfo.objects.all().update(age=999)
  return HttpResponse("更新成功！")

def get_user_list(request):
  user_list = models.UserInfo.objects.all()
  user_list_arr = []
  for user in user_list:
    user_dict = {
      "id": user.id,
      "name": user.name,
      "password": user.password,
      "age": user.age
    }
    user_list_arr.append(user_dict)
  return JsonResponse({
    "code": 200,
    "message": "查询成功",
    "data": user_list_arr
  })

def get_user_list_using_sql(request):
  user_list = models.UserInfo.objects.raw("select * from app_01_userinfo")
  user_list_arr = []
  for user in user_list:
    user_dict = {
      "id": user.id,
      "name": user.name,
      "password": user.password,
      "age": user.age
    }
    user_list_arr.append(user_dict)
  return JsonResponse({
    "code": 200,
    "message": "查询成功",
    "data": user_list_arr
  })

def get_user_count(request):
  user_count = models.UserInfo.objects.count()
  return JsonResponse({
    "code": 200,
    "message": "查询成功",
    "data": user_count
  })